<template>
	<view class="box">
		<moveTranslate x="90"></moveTranslate>
		<view class="img-logo text-center">
			<image src="../../static/logo-title.png" mode=""></image>
		</view>
		<view class="mlr-36" style="margin-top: 136rpx;">
			<view class="img text-center ptb-30 " @click="getCode">
				<!-- <button type="primary" open-type="getUserInfo" @getuserinfo="mpGetUserInfo" class="button">登录</button> -->
				<view class="center radius-20 ptb-15 white "  style="background:#00C800;width:500rpx;margin-left:100rpx;height: 80rpx;" >
					<!-- <image src="@/static/wx-w.png" style="width:50rpx;height:40rpx;" mode=""></image> -->
					<view class="ml-10">
						{{$t('login.yijiandenglu')}}
					</view>
				</view>
			</view>
      <!-- <button open-type="getUserInfo|agreePrivacyAuthorization" @getuserinfo="handleGetUserInfo" @agreeprivacyauthorization="handleAgreePrivacyAuthorization" style="background:#00C800;width:500rpx;margin-left:100rpx;height: 80rpx;line-height: 80rpx;color: #fff;font-size: 28rpx;">一键快速登录</button> -->
			<view class="text-center size-28 gray-2 mt-20 " :class="{'rtl-text': lang=='uey'}"  @click="goLogin">
				{{$t('login.dengluzhuce')}}
			</view>
		</view>
    
    <!-- @close="close" @open="open" -->
    <!-- <u-popup :show="showPopup" mode="center">
      <view>为保证应用的正常运行，小程序需要获取您的部分隐私权限，请查看隐私协议并同意！</view>
      <button @tap="handleOpenPrivacyContract">查看隐私协议</button>
      <button id="agree-btn" open-type="agreePrivacyAuthorization" bindagreeprivacyauthorization="handleAgreePrivacyAuthorization">同意</button>
    </u-popup> -->
	</view>
</template>

<script>
  import {
  	mapState
  } from 'vuex';
	var _this;
	
	export default {
		data() {
			return {
				// lang: this.$store.state.lang,
				cid:'',
        showPopup: false,
        // 司机id
        sid: ''
			}
		},
		onLoad(e) {
			_this = this;
			// #ifdef APP-PLUS
			console.log(plus.push.getClientInfo().clientid)
			this.cid = plus.push.getClientInfo().clientid
			// #endif
      
      if(e.scene) {
        this.sid = e.scene
        console.log(this.sid, this.token, this.userInfo)
        if(this.token && this.userInfo) {
          this.autoOrder()
        }
      }
      
      // uni.getPrivacySetting({
      //   success: res => {
      //     console.log(res)
      //     if (res.needAuthorization) {
      //       this.showPopup = true
      //     }
      //   }
      // })
		},
		computed: {
			...mapState(['userInfo','token',"lang"]),
		},
		methods: {
      handleOpenPrivacyContract() {
        uni.openPrivacyContract({})
      },
			goLogin() {
				uni.navigateTo({
					url: '/pages/login/index?sid='+this.sid
				})
			},
      handleAgreePrivacyAuthorization() {
        console.log('同意协议')
      },
      // 自动下单
      autoOrder() {
        uni.reLaunch({
        	url: '/pages/home/index_sfc?tag=true&type=1&sid='+this.sid
        })
      },
			wxLogin() {
				let that = this
				// if(!this.check){
				// 	return uni.showToast({
				// 		title:'请先阅读协议',
				// 		icon:'none'
				// 	})
				// }
				uni.getUserProfile({
					desc: that.$t('login.wanshanxinxix'), //必填，声明获取用户个人信息后的用途，不超过30个字符
					success: (res) => {
						console.log(res)
						// uni.showToast({
						// 	icon: "none",
						// 	title: '申请成功'
						// })
						setTimeout(function() {
							console.log("走着")
							that.getCode();
						}, 400)
			
					},
					fail: (err) => {
						console.log(err)
						uni.showToast({
							icon: "none",
							title: that.$t('login.yonghujujue')
						})
					}
				})
			},
			getCode(){
				uni.showLoading({
					title:_this.$t('login.dengluzhong')
				})
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes);
						_this.$http('/api/login/setOpenid', {
							code: loginRes.code,
							type:1
						}, "POST").then(data => {
							_this.isBand(data.openid)
						})
					},
          fail: (err) => {
            uni.showToast({
              title:_this.$t('login.weixinshouquanshibai'),
              icon: 'none'
            })
          }
				});
			},
      handleGetUserInfo(e) {
        this.getCode();
      },
			mpGetUserInfo() {
        
			},
			isBand(openid){
				this.$http('/api/login/isBand', {
					openid: openid,
					type:1,
				}, "POST").then(data => {
					uni.hideLoading()
					console.log(openid,"id",data);
					// _this.$store.commit('getUserInfo', data.userinfo)
					if(data.is_band == 1){
						_this.getinfo(openid)
					}else{
						uni.showToast({
								title: '',
								icon:'none'
						})
						setTimeout(() => {
							uni.reLaunch({
									url: '/pages/login/phone?sid=' + _this.sid + '&openid='+openid
							})
						}, 500)
					}
				})
			},
			getinfo(openid) {
				console.log("走着来")
				this.$http('/api/login/wechatLogin', {
					openid: openid,
					type:1,
				}, "POST").then(data => {
					console.log(data);
					_this.$store.commit('getUserInfo', data.userinfo)
					setTimeout(() => {
						uni.showToast({
							title:_this.$t('login.qingxianbangdingtel'),
							icon:'none'
						})
					}, 100)
          if(_this.sid) {
            _this.autoOrder()
          } else {
            if (data.userinfo.mobile) {
              uni.reLaunch({
                url: "/pages/tabbar/home"
              })
            } 
          }
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

	/deep/.u-btn {
		height: 96rpx !important;
	}

	.box {
		width: 100%;
		height: 100vh;
		// background: url(../../static/indexBg.png) no-repeat;
		background-size: 100% 100%;
		overflow: hidden;

		.img-logo{
			margin-top: 130rpx;
			image{
				width: 292rpx;
				height: 292rpx;
			}
		}

		.img {
			position: relative;

			.button {
				width: 100%;
				position: absolute;
				opacity: 0;
			}

			image {
				width: 92rpx;
				height: 92rpx;
			}
		}
	}
</style>
